package com.zaydanwu.cla.controller.students;

import com.zaydanwu.cla.model.Students;
import com.zaydanwu.cla.model.result.ApiResult;
import com.zaydanwu.cla.service.StudentsService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @BelongsProject: cla
 * @BelongsPackage: com.zaydanwu.cla.controller.students
 * @Author: zaydanwu
 * @CreateTime: 2018-12-09 17:06
 * @Description:
 */
@RestController
@RequestMapping("/cla/api")
@CrossOrigin
public class StudentsController {

    @Resource
    private StudentsService studentsService;

    @RequestMapping("/getStudentMessage")
    public ApiResult<Students> getStudentMessage(@RequestParam String name) {
        if (StringUtils.isEmpty(name)) {
            return ApiResult.failure("1000", "无对应的学生信息");
        }
        Students studentMessage = studentsService.getStudentMessage(name);
        studentMessage.setPassword(null);
        return ApiResult.success(studentMessage);
    }

    @RequestMapping("/getStudentMessageByStuNo")
    public ApiResult<Students> getStudentMessageByStuNo(@RequestParam Long stuNo) {
        if (Objects.isNull(stuNo)) {
            return ApiResult.failure("1000", "无对应的学生信息");
        }
        Students studentMessage = studentsService.getStudentMessageByStuNo(stuNo);
        studentMessage.setPassword(null);
        return ApiResult.success(studentMessage);
    }

    @RequestMapping("/getStudentMessageByStuNoAndPwd")
    public ApiResult<Students> getStudentMessageByStuNoAndPwd(@RequestParam Long stuNo, @RequestParam String password) {
        if (Objects.isNull(stuNo)) {
            return ApiResult.failure("1000", "无对应的学生信息");
        }
        Students studentMessage = studentsService.getStudentMessage(stuNo, password);
        studentMessage.setPassword(null);
        return ApiResult.success(studentMessage);
    }

    @RequestMapping("/updatePassword")
    public ApiResult<Object> updatePassword(@RequestParam Long stuNo,
                                            @RequestParam String phoneNumber,
                                            @RequestParam String newPassword) {
        if (Objects.isNull(stuNo)) {
            return ApiResult.failure("1000", "无对应的学生信息");
        }
        return ApiResult.success(studentsService.updatePassword(stuNo, phoneNumber, newPassword));
    }

//    GenericTokenParser
//    Reflector
//    Proxy
}
